//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "AverageBMACoupon.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Indexes/BMAIndex.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Indexes/InterestRateIndex.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Cashflows/FloatingRateCouponPricer.h>
#include <gen/QL/Cashflows/FloatingRateCoupon.h>
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CAverageBMACoupon::CAverageBMACoupon (DateTime paymentDate, Double nominal, DateTime startDate, DateTime endDate, Cephei::QL::Indexes::IBMAIndex^ index, Microsoft::FSharp::Core::FSharpOption<Double>^ gearing, Microsoft::FSharp::Core::FSharpOption<Double>^ spread, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodEnd, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IDayCounter^>^ dayCounter, Cephei::QL::Cashflows::IFloatingRateCouponPricer^ QL_Pricer) : CFloatingRateCoupon(CAverageBMACoupon::typeid)
{
    CBMAIndex^ _Cindex;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phAverageBMACoupon = NULL;
#endif
        QuantLib::Date _paymentDate = (QuantLib::Date)ValueHelper::Convert (paymentDate);
        QuantLib::Real _nominal = (QuantLib::Real)ValueHelper::Convert (nominal);
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate);
        QuantLib::Date _endDate = (QuantLib::Date)ValueHelper::Convert (endDate);
        _Cindex = safe_cast<CBMAIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::BMAIndex>& _index = static_cast<boost::shared_ptr<QuantLib::BMAIndex>&> (_Cindex->GetShared ()); 
        QuantLib::Real _gearing = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (gearing) ? (QuantLib::Real)ValueHelper::Convert (gearing->Value) : 1.0); //4
        QuantLib::Spread _spread = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (spread) ? (QuantLib::Spread)ValueHelper::Convert (spread->Value) : 0.0); //4
        QuantLib::Date _refPeriodStart = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refPeriodStart) ? (QuantLib::Date)ValueHelper::Convert (refPeriodStart->Value) : QuantLib::Date()); //4
        QuantLib::Date _refPeriodEnd = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refPeriodEnd) ? (QuantLib::Date)ValueHelper::Convert (refPeriodEnd->Value) : QuantLib::Date()); //4
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IDayCounter^>::IsSome::get (dayCounter))
        {
            _CdayCounter = safe_cast<CDayCounter^> (dayCounter->Value);
            _CdayCounter->Lock();
        }
        QuantLib::DayCounter& _dayCounter = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IDayCounter^>::IsSome::get (dayCounter) ? static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()) : QuantLib::DayCounter()); //1
        _ppAverageBMACoupon = new boost::shared_ptr<QuantLib::AverageBMACoupon> (new QuantLib::AverageBMACoupon ( _paymentDate,  _nominal,  _startDate,  _endDate,  _index,  _gearing,  _spread,  _refPeriodStart,  _refPeriodEnd,  _dayCounter ));
        CFloatingRateCouponPricer^ _CQL_Pricer = safe_cast<CFloatingRateCouponPricer^> (QL_Pricer);
        boost::shared_ptr<QuantLib::FloatingRateCouponPricer>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::FloatingRateCouponPricer>&> (_CQL_Pricer->GetShared ());
        (*_ppAverageBMACoupon)->setPricer (_QL_Pricer);
        SetFloatingRateCoupon (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppAverageBMACoupon));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cindex != nullptr) _Cindex->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Cashflows::CAverageBMACoupon::CAverageBMACoupon (boost::shared_ptr<QuantLib::AverageBMACoupon>& childNative, Object^ owner) : CFloatingRateCoupon(CAverageBMACoupon::typeid)
{
#ifdef HANDLE
	_phAverageBMACoupon = NULL;
#endif
	_ppAverageBMACoupon = &childNative;
    _ppFloatingRateCoupon = new boost::shared_ptr<QuantLib::FloatingRateCoupon> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppAverageBMACoupon));
}
Cephei::QL::Cashflows::CAverageBMACoupon::CAverageBMACoupon (QuantLib::AverageBMACoupon& childNative, Object^ owner) : CFloatingRateCoupon(CAverageBMACoupon::typeid)
{
#ifdef HANDLE
	_phAverageBMACoupon = NULL;
#endif
	_ppAverageBMACoupon = new boost::shared_ptr<QuantLib::AverageBMACoupon> (&childNative);
    _ppFloatingRateCoupon = new boost::shared_ptr<QuantLib::FloatingRateCoupon> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppAverageBMACoupon));
    _AverageBMACouponOwner = owner;
    _FloatingRateCouponOwner = owner;
}

Cephei::QL::Cashflows::CAverageBMACoupon::CAverageBMACoupon (CAverageBMACoupon^ copy) : CFloatingRateCoupon(CAverageBMACoupon::typeid)
{
#ifdef HANDLE
	_phAverageBMACoupon = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAverageBMACoupon = new boost::shared_ptr<QuantLib::AverageBMACoupon> (copy->GetShared());
        _ppFloatingRateCoupon = new boost::shared_ptr<QuantLib::FloatingRateCoupon> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppAverageBMACoupon));
    }
}
Cephei::QL::Cashflows::CAverageBMACoupon::CAverageBMACoupon (System::Type^ t) : CFloatingRateCoupon(CAverageBMACoupon::typeid)
{
#ifdef HANDLE
	_phAverageBMACoupon = NULL;
#endif
	if (!t->IsSubclassOf(CAverageBMACoupon::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CAverageBMACoupon::CAverageBMACoupon (QuantLib::Handle<QuantLib::AverageBMACoupon>& childNative, Object^ owner)  : CFloatingRateCoupon(CAverageBMACoupon::typeid)
{
	_phAverageBMACoupon = &childNative;
	_ppAverageBMACoupon = &static_cast<boost::shared_ptr<QuantLib::AverageBMACoupon>>(childNative.currentLink());
    _ppFloatingRateCoupon = new boost::shared_ptr<QuantLib::FloatingRateCoupon> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppAverageBMACoupon));
    _AverageBMACouponOwner = owner;
}
Cephei::QL::Cashflows::CAverageBMACoupon::CAverageBMACoupon (QuantLib::Handle<QuantLib::AverageBMACoupon> childNative)  : CFloatingRateCoupon(CAverageBMACoupon::typeid)
{
	_phAverageBMACoupon = &childNative;
	_ppAverageBMACoupon = &static_cast<boost::shared_ptr<QuantLib::AverageBMACoupon>>(childNative.currentLink());
    _ppFloatingRateCoupon = new boost::shared_ptr<QuantLib::FloatingRateCoupon> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppAverageBMACoupon));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CAverageBMACoupon::CAverageBMACoupon (QuantLib::AverageBMACoupon childNative)  : CFloatingRateCoupon(CAverageBMACoupon::typeid)
{
#ifdef HANDLE
	_phAverageBMACoupon = NULL;
#endif
	_ppAverageBMACoupon = new boost::shared_ptr<QuantLib::AverageBMACoupon> (new QuantLib::AverageBMACoupon (childNative));
    _ppFloatingRateCoupon = new boost::shared_ptr<QuantLib::FloatingRateCoupon> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppAverageBMACoupon));
}
#endif

Cephei::QL::Cashflows::CAverageBMACoupon::~CAverageBMACoupon ()
{
    if (_ppAverageBMACoupon != NULL)
    {
	    delete _ppAverageBMACoupon;
        _ppAverageBMACoupon = NULL;
    }
}
Cephei::QL::Cashflows::CAverageBMACoupon::!CAverageBMACoupon ()
{
    if (_ppAverageBMACoupon != NULL)
    {
	    delete _ppAverageBMACoupon;
    }
}
QuantLib::AverageBMACoupon& Cephei::QL::Cashflows::CAverageBMACoupon::GetReference ()
{
    if (_ppAverageBMACoupon == NULL) throw gcnew NativeNullException ();
	return **_ppAverageBMACoupon;
}
boost::shared_ptr<QuantLib::AverageBMACoupon>& Cephei::QL::Cashflows::CAverageBMACoupon::GetShared ()
{
    if (_ppAverageBMACoupon == NULL) throw gcnew NativeNullException ();
	return *_ppAverageBMACoupon;
}
QuantLib::AverageBMACoupon* Cephei::QL::Cashflows::CAverageBMACoupon::GetPointer ()
{
    if (_ppAverageBMACoupon == NULL) throw gcnew NativeNullException ();
	return &**_ppAverageBMACoupon;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AverageBMACoupon>& Cephei::QL::Cashflows::CAverageBMACoupon::GetHandle ()
{
	if (_phAverageBMACoupon == NULL)
	{
		_phAverageBMACoupon = new Handle<QuantLib::AverageBMACoupon> (*_ppAverageBMACoupon);
	}
	return *_phAverageBMACoupon;
}
#endif
bool Cephei::QL::Cashflows::CAverageBMACoupon::HasNative () 
{
	return (_ppAverageBMACoupon != NULL);
}

Double Cephei::QL::Cashflows::CAverageBMACoupon::ConvexityAdjustment::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppAverageBMACoupon)->convexityAdjustment ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Cashflows::CAverageBMACoupon::FixingDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppAverageBMACoupon)->fixingDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<DateTime>^ Cephei::QL::Cashflows::CAverageBMACoupon::FixingDates::get ()
{
    try
    {
    	std::vector<QuantLib::Date> _rv = (std::vector<QuantLib::Date>)(*_ppAverageBMACoupon)->fixingDates ( );   
        Cephei::IVector<DateTime>^ _nrv = gcnew CoVector<DateTime> (gcnew CDateTimeVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CAverageBMACoupon::IndexFixing::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppAverageBMACoupon)->indexFixing ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Cashflows::CAverageBMACoupon::IndexFixings::get ()
{
    try
    {
    	std::vector<QuantLib::Rate> _rv = (std::vector<QuantLib::Rate>)(*_ppAverageBMACoupon)->indexFixings ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::IAverageBMACoupon^ Cephei::QL::Cashflows::CAverageBMACoupon_Factory::Create (DateTime paymentDate, Double nominal, DateTime startDate, DateTime endDate, Cephei::QL::Indexes::IBMAIndex^ index, Microsoft::FSharp::Core::FSharpOption<Double>^ gearing, Microsoft::FSharp::Core::FSharpOption<Double>^ spread, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodEnd, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IDayCounter^>^ dayCounter, Cephei::QL::Cashflows::IFloatingRateCouponPricer^ QL_Pricer)
{
    return gcnew CAverageBMACoupon ( paymentDate,  nominal,  startDate,  endDate,  index,  gearing,  spread,  refPeriodStart,  refPeriodEnd,  dayCounter,  QL_Pricer);
}
